package com.joysuch.wwyt;

import com.google.common.base.Joiner;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.licence.exception.LicenceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * web层异常拦截
 *
 * @author qingzhou
 */
@ResponseBody
@ControllerAdvice
@Slf4j
public class WebExceptionHandler {

    /**
     * 处理接口验证异常
     *
     * @param request 请求
     * @param e       验证异常
     * @return 错误提示
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultBean handleMethodArgumentNotValidException(HttpServletRequest request, MethodArgumentNotValidException e) {
//        log.error("请求" + request.getRequestURL() + "执行异常:", e);
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        Set<String> collect = allErrors.stream().map(DefaultMessageSourceResolvable::getDefaultMessage)
                .collect(Collectors.toSet());
        return ResultBean.wrapErrorResultDefault(Joiner.on(",").join(collect));
    }

    /**
     * 处理接口传参无法读取异常
     *
     * @param request 请求
     * @param e       异常
     * @return 错误提示
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResultBean handleHttpMessageNotReadableException(HttpServletRequest request, HttpMessageNotReadableException e) {
//        log.error("请求" + request.getRequestURL() + "执行异常:", e);
        Throwable mostSpecificCause = e.getMostSpecificCause();
        String exceptionName = mostSpecificCause.getClass().getName();
        String message = mostSpecificCause.getMessage();
        return ResultBean.wrapErrorResultDefault(exceptionName + message);
    }

    /**
     * 处理参数绑定异常
     *
     * @param request 请求
     * @param e       异常
     * @return 错误提示
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(BindException.class)
    public ResultBean handleBindException(HttpServletRequest request, BindException e) {
//        log.error("请求" + request.getRequestURL() + "执行异常:", e);
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        Set<String> collect = allErrors.stream().map(DefaultMessageSourceResolvable::getDefaultMessage)
                .collect(Collectors.toSet());
        return ResultBean.wrapErrorResultDefault(Joiner.on(",").join(collect));
    }

    /**
     * 处理Assert IllegalArgumentException
     *
     * @param e       异常
     * @return 错误提示
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(IllegalArgumentException.class)
    public ResultBean handleIllegalArgumentException(IllegalArgumentException e) {
        return ResultBean.wrapErrorResultDefault(e.getMessage());
    }

    /**
     * licence 服务端异常
     *
     * @param e       异常
     * @return 通用错误提示
     */
    @ExceptionHandler(LicenceException.class)
    public ResultBean handleLicenceException(LicenceException e) {
        return ResultBean.wrapErrorResult(e.getCode(), e.getMsg());
    }

    /**
     * 处理服务端异常
     *
     * @param request 请求
     * @param e       异常
     * @return 通用错误提示
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(Exception.class)
    public ResultBean handleException(HttpServletRequest request, Exception e) {
        log.error("请求" + request.getRequestURL() + "执行异常:", e);
        return ResultBean.wrapErrorResult(-90, "操作失败，请稍后重试");
    }

//    /**
//     * api接口异常
//     * @param ex
//     * @return
//     */
//    @ResponseStatus(HttpStatus.OK)
//    @ExceptionHandler(ApiV1ExceptionCodeMsg.class)
//    public ResultBean ApiV1ExceptionCodeMsgHandler(ApiV1ExceptionCodeMsg ex){
//        log.info("[ApiV1ExceptionCodeMsgHandler]", ex);
//        return ResultBean.wrapErrorResult(ex.getCode(), ex.getMessage());
//    }
}
